package com.project.service;

import com.project.common.utils.DeleteFileUtil;
import com.project.common.utils.FileUtils;
import com.project.common.utils.Util;
import com.project.dao.RecruitInfoMapper;
import com.project.entity.Image;
import com.project.entity.RecruitInfo;
import com.project.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.List;

/**
 * @Author: TateBrown
 * @date: 2018/5/26 14:56
 * @param:
 * @return:
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class RecruitInfoServiceImpl extends BaseService implements RecruitInfoService {

    @Autowired
    private ImagesService imagesService;
    @Autowired
    private  RecruitInfoMapper recruitInfoMapper;


    @Override
    public int modifyRecruitInfoById(RecruitInfo recruitInfo) throws IOException {

        recruitInfoMapper.updateByPrimaryKeySelective(recruitInfo);

        //公司工作环境
        if (!StringUtils.isEmpty(recruitInfo.getWorkingpicBase64())) {
            //1. 获取root上级目录
            String rootPath = Util.getWebRootURL(this);

            //2. 获取下级目录
            String subPath = configParameters.getWorkingPicSubPath();
            //3. 获取文件格式
            String ext = Util.getBase64StringExt(recruitInfo.getWorkingpicBase64());
            //4. 设置文件名称
            String fileNamePrefix = "workingpic_";
            String fileName = fileNamePrefix + recruitInfo.getId() + "." + ext;
            //5. 创建文件
            String outBase64Str = Util.getBase64StringData(recruitInfo.getWorkingpicBase64());
            FileUtils.writeDecodeBase64StrToFile(rootPath + subPath + fileName, outBase64Str);

            Image image = new Image();
            image.setPath(fileName);
            if (recruitInfo.getWorkingpic() == null) { //若无上传图片
                image.setCategory(3); //工作环境
                imagesService.add(image);
                recruitInfo.setWorkingpic(image.getId());
                recruitInfoMapper.updateByPrimaryKeySelective(recruitInfo);
            } else {//若先前已上传图片
                image.setId(recruitInfo.getWorkingpic());
                imagesService.update(image);
            }
        }
        return 1;
    }


    @Override
    public int add(RecruitInfo recruitInfo) throws IOException {

        recruitInfoMapper.insertSelective(recruitInfo);
        //工作环境
        if (!StringUtils.isEmpty(recruitInfo.getWorkingpicBase64())) {
            //1. 获取root上级目录
            String rootPath = Util.getWebRootURL(this);
            //2. 获取下级目录
            String subPath = configParameters.getWorkingPicSubPath();
            //3. 获取文件格式
            String ext = Util.getBase64StringExt(recruitInfo.getWorkingpicBase64());
            //4. 设置文件名称
            String fileNamePrefix = "workingpic_";
            String fileName = fileNamePrefix + recruitInfo.getId() + "." + ext;
            //5. 创建文件
            String outBase64Str = Util.getBase64StringData(recruitInfo.getWorkingpicBase64());
            FileUtils.writeDecodeBase64StrToFile(rootPath + subPath + fileName, outBase64Str);

            //插入图片记录
            Image image = new Image();
            image.setPath(fileName);
            image.setCategory(3); //工作环境
            imagesService.add(image);
            System.out.println(image.getId());

            //插入imageId
            RecruitInfo recruitInfo1 = new RecruitInfo();
            recruitInfo1.setId(recruitInfo.getId());
            recruitInfo1.setWorkingpic(image.getId());
            modifyRecruitInfoById(recruitInfo1);
        }
        return 1;
    }

    @Override
    public int remove(Integer id) {
        RecruitInfo recruitInfo = findRecruitInfoById(id);
        if (recruitInfo.getWorkingpic() != null) {        //若包含图片 则删除图片

            Image image = imagesService.findById(recruitInfo.getWorkingpic());
            //1. 获取root上级目录
            String rootPath = Util.getWebRootURL(this);
            //2. 找到绝对路径
            String path = rootPath + configParameters.getWorkingPicSubPath() + image.getPath();
            //根据绝对路径删除图片
            DeleteFileUtil.delete(path);

            //删除图片表记录
            imagesService.delete(image.getId());
        }
        //删除照片信息表
        recruitInfoMapper.deleteByPrimaryKey(id);
        return 1;
    }

    @Override
    public List<RecruitInfo> findByCondition(RecruitInfo recruitInfo) {
        return recruitInfoMapper.selectByCondition(recruitInfo);
    }

    @Override
    public RecruitInfo findRecruitInfoById(int id) {
        return recruitInfoMapper.selectByPrimaryKey(id);
    }
}
